package com.qust.rest.interceptor;

import com.qust.securityapi.service.CheckService;
import com.qust.securityapi.service.HasRoles;
import com.qust.securityapi.threadLocal.AuthThreadLocal;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * desc: 登录验证拦截器
 * @author Frank KONG
 * @version 1.0
 * @date 2021/11/22 19:56
 */
public class AuthInterceptor implements HandlerInterceptor {

    private final CheckService checkService;

    public AuthInterceptor(CheckService checkService){
        this.checkService = checkService;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        HandlerMethod method = (HandlerMethod) handler;
        HasRoles annotation = method.getMethodAnnotation(HasRoles.class);
        String[] roles = annotation.roles();
        for (String role : roles) {
            System.out.println(role);
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        AuthThreadLocal.clearAuth();
    }
}
